<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar" :rules="{ required: '必传' }">
        <el-upload
          :action="dataForm.upload"
          :show-file-list="false"
          :before-upload="beforeUploadHandle"
          :on-success="successHandle">
          <el-image v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar" style="max-width: 200px"></el-image>
          <el-icon v-else :size="30">
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="认证" prop="authentication">
        <el-radio-group v-model="dataForm.authentication">
          <el-radio :label="0">未认证</el-radio>
          <el-radio :label="1">已认证</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realname">
        <el-input v-model="dataForm.realname" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份ID" prop="idNo">
        <el-input v-model="dataForm.idNo" placeholder="身份ID"></el-input>
      </el-form-item>
      <el-form-item label="组织" prop="company">
        <el-input v-model="dataForm.company" placeholder="组织"></el-input>
      </el-form-item>
      <el-form-item label="余额" prop="balance">
        <el-col :span="12">
          <el-input disabled="disabled" v-model="dataForm.balance" placeholder="余额"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="欠款限额" prop="debtLimit">
        <el-col :span="6">
          <el-input type="number" max="99999999.99" v-model="dataForm.debtLimit" placeholder="欠款限额"
                    @input="dataForm.debtLimit = Math.min(dataForm.debtLimit, 99999999.99)"
          ></el-input>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="2">已使用</el-col>
        <el-col :span="4">{{ dataForm.debtLimitUse }}</el-col>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import {getToken} from "@/utils/cache";
import {IObject} from "@/types/interface";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  username: "",
  realname: "",
  idNo: "",
  company: "",
  mobile: "",
  avatar: "",
  createDate: "",
  balance: "",
  debtLimit: 0,
  debtLimitUse: "",
  authentication: 0,
  upload: ""
});

const rules = ref({
  username: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  mobile: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  avatar: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  balance: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  debtLimit: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  authentication: [{required: true, message: "必填项不能为空", trigger: "blur"}]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.upload = `${window.SITE_CONFIG['apiURLUpload']}?token=${getToken()}&type=user`

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/user/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 上传之前
const beforeUploadHandle = (file: IObject) => {
  if (file.type !== "image/jpg" && file.type !== "image/jpeg" && file.type !== "image/png" && file.type !== "image/gif") {
    ElMessage.error("只支持jpg、png、gif格式文件！");
    return false;
  }
};

// 上传成功
const successHandle = (res: IObject, file: IObject, list: IObject[]) => {
  dataForm.avatar = res.data.url;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/user", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
